package cn.silver.framework.jpa.bean;

import cn.silver.framework.core.model.DictModel;
import cn.silver.framework.web.model.TreeModel;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @author zhouxiaobo
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@Schema(name = "TreeModel", description = "树结构对象")
public abstract class TreeBean<T> extends BusBean {

    @Schema(title = "上级节点")
    private String pid;

    @Schema(title = "上级节点名称")
    private String pname;

    @Schema(title = "节点编码")
    private String node;

    @Schema(title = "上级节点编码")
    private String pnode;

    @Schema(title = "是否有子节点")
    private Boolean hasChild;

    @Schema(title = "子节点集合")
    private List<T> children;

    public void addChildren(List<T> children) {
        if (this.children == null) {
            this.children = children;
        } else {
            this.children.addAll(children);
        }
    }

    @Override
    public DictModel convertDict() {
        return new DictModel(this.getId(), this.getName(), this.getCode(), this.pid, this.getSort(), this.getRemark());
    }

    public TreeModel convertTree() {
        return new TreeModel(this.getId(), this.pid, this.getCode(), this.getName(), this.getName());
    }
}
